<?php
    /**
     * Bridge Class Which send Requests to crowdvox script to login and register.
     * Requries curl 
     * json_decode method
     * @throw CrowdvoxBridgeException
     */
    class CrowdvoxBridge
    {
        /**
         *
         * @var CrowdvoxBridgeRequest 
         */
        private $requestHandler;
        private $settings;
        function __construct($settings = array())
        {
            $settings = array_merge($this->getIniSettings(),$settings);
            $this->settings = $settings;
            $requireFields = array("crowdvox_url","access_token","cookieName");
            $missingFields = array();
            foreach($requireFields as $requireField)
            {
                if(!isset($this->settings[$requireField]))
                {
                    $missingFields[] = $requireField;
                }
            }
            if(count($missingFields) > 0)
            {
                throw new CrowdvoxBridgeException("Following settings field not found ".implode(",",$missingFields));
            }
        }
        
        function register($id,$username,$email,$otherInformation = array())
        {
            $data = compact("id","username","email","otherInformation");
            try
            {
                $response = $this->getRequestHandler()->post("/bridge/user_bridge/register", $data);
            }  catch (CrowdvoxBridgeRequestException $e)
            {
                throw new CrowdvoxBridgeException("Request Error : ".$e->getMessage(),$e->getCode(),$e);
            }
        }
        
        function login($id,$username,$email,$registrationDate,$otherInformation = array())
        {
            $data = compact("id","username","email","registrationDate","otherInformation");
            try
            {
                $response = $this->getRequestHandler()->post("/bridge/user_bridge/login", $data);
                if($response['loginToken'])
                {
                    $this->createConnectorCookie($response['loginToken']);
                }else
                {
                    throw new CrowdvoxBridgeException("Request Error".$e->getMessage());
                }
            }  catch (CrowdvoxBridgeRequestException $e)
            {
                throw new CrowdvoxBridgeException("Request Error".$e->getMessage());
            }
        }
        
        function delete()
        {
            
        }
        
        function logout()
        {
            $this->createConnectorCookie("",time() - (3600*4));
        }

        //Only considers case of forum on  main domain.
        //Sets cookie to be avaiable all subdomain and domain forum domain
        private function createConnectorCookie($value,$time = null)
        {
            $host = $_SERVER['HTTP_HOST'];
            $host = str_replace("www.", "", $host);
            if(!$time)
            {
                $time = time()+(3600*24*7);
            }
            setcookie($this->settings["cookieName"],$value,$time,"/",'.'.$host);
            setcookie($this->settings["cookieName"],$value,$time,"/",$host);
        }
        //return domain name of current script.
        private function getDomainName()
        {
            
        }
        
        private function getRequestHandler()
        {
            
            if(!$this->requestHandler)
            {
                require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'request.php';
                
                $this->requestHandler = new CrowdvoxBridgeRequest($this->settings["crowdvox_url"],$this->settings["access_token"]);
            }
            return $this->requestHandler ;
        }
        private function getIniSettings()
        {
            $iniFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'settings.ini';
            $iniSettings = array();
            if(file_exists($iniFile))
            {
                $iniSettings = parse_ini_file($iniFile);
                if(!$iniSettings)
                {
                    $iniSettings = array();
                }
            }
            return $iniSettings;
        }
    }
    
    class CrowdvoxBridgeException extends Exception
    {
        
    }
?>
